﻿using System;
using System.ComponentModel;
using UnityEngine;

namespace AutoAim.Config
{
    [Serializable]
    public class AutoAimConfig
    {
        #region 距离相关

        [Description("锁敌距离类型")] public AimTargetDistanceType distanceType = AimTargetDistanceType.Fixed;

        [Description("固定锁敌距离")] public float fixedLockDistance = 20f;

        [Description("枪械基础锁敌距离系数")] public float weaponDistanceMultiplier = 1.07f; // 距离 = 枪默认射程 * 此系数

        #endregion

        #region 锁定部位

        [Description("锁定部位")] public AimTargetPart targetPart = AimTargetPart.Head;

        #endregion

        #region 散射控制

        [Description("散布系数")] public float gunScatterMultiplier; // 0~1，0=无散布

        #endregion

        #region 激活方式

        [Description("锁敌按键行为")] public AimActivationMode activationMode = AimActivationMode.AlwaysOn;

        [Description("切换热键 (KeyCode)")] public KeyCode toggleHotkey = KeyCode.F8;

        #endregion

        #region 视野限制

        [Description("锁敌范围限制")] public AimVisibilityMode visibilityMode = AimVisibilityMode.IgnoreVisibility;

        // 可选：视野角度（锥体角度）
        [Description("视野角度（度）")] public float sightAngle = 90f;

        #endregion

        #region 性能与行为

        [Description("动态跳帧阈值 (FPS)")] public int frameSkipThreshold = 50;

        [Description("缓存清理间隔帧数")] public int cacheCleanupIntervalFrames = 30;

        #endregion

        #region 方法：加载/保存/重置

        /// <summary>
        /// 默认配置
        /// </summary>
        public static AutoAimConfig Default => new AutoAimConfig();

        /// <summary>
        /// 重置为默认值
        /// </summary>
        public void ResetToDefault()
        {
            var defaultConfig = Default;
            foreach (var prop in typeof(AutoAimConfig).GetProperties())
            {
                if (prop.CanWrite)
                    prop.SetValue(this, prop.GetValue(defaultConfig));
            }
        }

        #endregion

        #region 日志

        public bool writeLog;

        #endregion

        #region 模式选择 (新增)

        [Description("自瞄模式")] public AimMode aimMode = AimMode.HardLock;

        #endregion

        #region 吸附模式配置 (新增)

        [Description("吸附范围 (屏幕空间像素)")] public float adsorptionRange = 200f;

        [Description("吸附强度 (0-1)")] public float adsorptionStrength = 0.5f;

        [Description("吸附衰减模式")] public AdsorptionFalloff adsorptionFalloff = AdsorptionFalloff.Linear;

        #endregion

        /// <summary>
        /// 获取实际锁敌距离（根据距离类型）
        /// </summary>
        public float GetLockDistance(ItemAgent_Gun gun)
        {
            return distanceType switch
            {
                AimTargetDistanceType.Fixed => fixedLockDistance,
                AimTargetDistanceType.BasedOnWeapon => gun?.BulletDistance * weaponDistanceMultiplier ??
                                                       fixedLockDistance,
                _ => fixedLockDistance
            };
        }

        /// <summary>
        /// 检查是否应激活锁敌（根据当前输入状态）
        /// </summary>
        public bool ShouldActivate(CharacterMainControl player)
        {
            return activationMode switch
            {
                AimActivationMode.AlwaysOn => true,
                AimActivationMode.WhenAiming => player.IsInAdsInput,
                AimActivationMode.WhenNotAiming => !player.IsInAdsInput,
                // AimActivationMode.ToggleWithHotkey => player.ModState.AutoAimEnabled,
                _ => false
            };
        }

        /// <summary>
        /// 获取敌人目标位置（根据锁定部位）
        /// </summary>
        public Vector3 GetTargetPosition(CharacterMainControl enemy)
        {
            return targetPart switch
            {
                AimTargetPart.Head => GetEnemyHeadPosition(enemy),
                _ => enemy.transform.position + Vector3.up * 0.2f
            };
        }

        /// <summary>
        /// 获取敌人头部位置（复用原有逻辑）
        /// </summary>
        private static Vector3 GetEnemyHeadPosition(CharacterMainControl enemy)
        {
            if (enemy == null) return Vector3.zero;

            // 优先找 HeadCollider
            var headCollider = enemy.GetComponentInChildren<HeadCollider>(true);
            if (headCollider != null)
            {
                return headCollider.transform.position;
            }

            // 其次找 HelmatSocket
            if (enemy.characterModel != null && enemy.characterModel.HelmatSocket != null)
            {
                return enemy.characterModel.HelmatSocket.position;
            }

            // 最后兜底：角色中心上方偏移
            return enemy.transform.position + Vector3.up * 0.2f;
        }
    }
}